home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / libxpm / libxpm34.gz / libxpm34 / xpm-3.4 / sxpm / sxpm.c < prev    next >
C/C++ Source or Header  |  1994-03-14  |  18KB  |  663 lines

  1. /* Copyright 1989-94 GROUPE BULL -- See license conditions in file COPYRIGHT */
  2. /*****************************************************************************\
  3. * sxpm.c:                                                                     *
  4. *                                                                             *
  5. *  Show XPM File program                                                      *
  6. *                                                                             *
  7. *  Developed by Arnaud Le Hors                                                *
  8. \*****************************************************************************/
  9.  
  10. #ifdef VMS
  11. #include "sys$library:stdio.h"
  12. #include "decw$include:Xlib.h"
  13. #include "decw$include:Intrinsic.h"
  14. #include "decw$include:Shell.h"
  15. #include "decw$include:shape.h"
  16. #else
  17. #include <stdio.h>
  18. #include <X11/StringDefs.h>
  19. #include <X11/Intrinsic.h>
  20. #include <X11/Shell.h>
  21. #include <X11/extensions/shape.h>
  22. #endif
  23.  
  24. #include "xpm.h"
  25.  
  26. /* XPM */
  27. /* plaid pixmap */
  28. static char *plaid[] = {
  29.     /* width height ncolors chars_per_pixel */
  30.     "22 22 4 2 XPMEXT",
  31.     /* colors */
  32.     "   c red     m white  s light_color",
  33.     "Y  c green    m black  s lines_in_mix",
  34.     "+  c yellow    m white  s lines_in_dark",
  35.     "x         m black  s dark_color",
  36.     /* pixels */
  37.     "x   x   x x x   x   x x x x x x + x x x x x ",
  38.     "  x   x   x   x   x   x x x x x x x x x x x ",
  39.     "x   x   x x x   x   x x x x x x + x x x x x ",
  40.     "  x   x   x   x   x   x x x x x x x x x x x ",
  41.     "x   x   x x x   x   x x x x x x + x x x x x ",
  42.     "Y Y Y Y Y x Y Y Y Y Y + x + x + x + x + x + ",
  43.     "x   x   x x x   x   x x x x x x + x x x x x ",
  44.     "  x   x   x   x   x   x x x x x x x x x x x ",
  45.     "x   x   x x x   x   x x x x x x + x x x x x ",
  46.     "  x   x   x   x   x   x x x x x x x x x x x ",
  47.     "x   x   x x x   x   x x x x x x + x x x x x ",
  48.     "          x           x   x   x Y x   x   x ",
  49.     "          x             x   x   Y   x   x   ",
  50.     "          x           x   x   x Y x   x   x ",
  51.     "          x             x   x   Y   x   x   ",
  52.     "          x           x   x   x Y x   x   x ",
  53.     "x x x x x x x x x x x x x x x x x x x x x x ",
  54.     "          x           x   x   x Y x   x   x ",
  55.     "          x             x   x   Y   x   x   ",
  56.     "          x           x   x   x Y x   x   x ",
  57.     "          x             x   x   Y   x   x   ",
  58.     "          x           x   x   x Y x   x   x ",
  59.     "bullshit",
  60.     "XPMEXT ext1 data1",
  61.     "XPMEXT ext2",
  62.     "data2_1",
  63.     "data2_2",
  64.     "XPMEXT",
  65.     "foo",
  66.     "",
  67.     "XPMEXT ext3",
  68.     "data3",
  69.     "XPMENDEXT"
  70. };
  71.  
  72. #define win XtWindow(topw)
  73. #define dpy XtDisplay(topw)
  74. #define screen XtScreen(topw)
  75. #define root XRootWindowOfScreen(screen)
  76. #define xrdb XtDatabase(dpy)
  77. static Colormap colormap;
  78.  
  79. void Usage();
  80. void ErrorMessage();
  81. void Punt();
  82. #ifdef __STDC__
  83. void kinput(Widget widget, char *tag, XEvent *xe, Boolean *b);
  84. #else
  85. void kinput();
  86. #endif
  87.  
  88. #define IWIDTH      50
  89. #define IHEIGHT     50
  90.  
  91. typedef struct _XpmIcon {
  92.     Pixmap pixmap;
  93.     Pixmap mask;
  94.     XpmAttributes attributes;
  95. }        XpmIcon;
  96.  
  97. static char **command;
  98. static Widget topw;
  99. static XpmIcon view, icon;
  100. static XrmOptionDescRec options[] = {
  101.     {"-hints", ".hints", XrmoptionNoArg, (XtPointer) "True"},
  102.     {"-icon", ".icon", XrmoptionSepArg, (XtPointer) NULL},
  103. };
  104.  
  105. main(argc, argv)
  106.     int argc;
  107.     char **argv;
  108. {
  109.     int ErrorStatus;
  110.     unsigned int verbose = 0;    /* performs verbose output */
  111.     unsigned int stdinf = 1;    /* read from stdin */
  112.     unsigned int stdoutf = 0;    /* write to stdout */
  113.     unsigned int nod = 0;    /* no display */
  114.     unsigned int nom = 0;    /* no mask display */
  115.     unsigned int incResize = 0;
  116.     unsigned int resize = 0;
  117.     unsigned int w_rtn;
  118.     unsigned int h_rtn;
  119.     char *input = NULL;
  120.     char *output = NULL;
  121.     char *iconFile = NULL;
  122.     unsigned int numsymbols = 0;
  123.     XpmColorSymbol symbols[10];
  124.     char *stype;
  125.     XrmValue val;
  126.     unsigned long valuemask = 0;
  127.     int n;
  128.     Arg args[4];
  129.  
  130. #ifdef Debug
  131.     char **data;
  132.     char *buffer;
  133. #endif
  134.  
  135.     topw = XtInitialize(argv[0], "Sxpm",
  136. #ifdef sgi
  137.             options, XtNumber(options), (Cardinal*)&argc, argv);
  138. #else
  139.             options, XtNumber(options), &argc, argv);
  140. #endif
  141.  
  142.     if (!topw) {
  143.     fprintf(stderr, "Sxpm Error... [ Undefined DISPLAY ]\n");
  144.     exit(1);
  145.     }
  146.     colormap = XDefaultColormapOfScreen(screen);
  147.  
  148.     /*
  149.      * geometry management
  150.      */
  151.  
  152.     if (XrmGetResource(xrdb, NULL, "sxpm.geometry", &stype, &val)
  153.     || XrmGetResource(xrdb, NULL, "Sxpm.geometry", &stype, &val)) {
  154.  
  155.     int flags;
  156.     int x_rtn;
  157.     int y_rtn;
  158.     char *geo = NULL;
  159.  
  160.     geo = (char *) val.addr;
  161.     flags = XParseGeometry(geo, &x_rtn, &y_rtn,
  162.                    (unsigned int *) &w_rtn,
  163.                    (unsigned int *) &h_rtn);
  164.  
  165.     if (!((WidthValue & flags) && (HeightValue & flags)))
  166.         resize = 1;
  167.  
  168.     } else
  169.     resize = 1;
  170.  
  171.     n = 0;
  172.     if (resize) {
  173.     w_rtn = 0;
  174.     h_rtn = 0;
  175.     XtSetArg(args[n], XtNwidth, 1);
  176.     n++;
  177.     XtSetArg(args[n], XtNheight, 1);
  178.     n++;
  179.     }
  180.     XtSetArg(args[n], XtNmappedWhenManaged, False);
  181.     n++;
  182.     XtSetArg(args[n], XtNinput, True);
  183.     n++;
  184.     XtSetValues(topw, args, n);
  185.  
  186.     if ((XrmGetResource(xrdb, "sxpm.hints", "", &stype, &val)
  187.      || XrmGetResource(xrdb, "Sxpm.hints", "", &stype, &val))
  188.     && !strcmp((char *) val.addr, "True")) {
  189.     /* gotcha */
  190.     incResize = 1;
  191.     resize = 1;
  192.     }
  193.     /*
  194.      * icon management
  195.      */
  196.  
  197.     if (XrmGetResource(xrdb, "sxpm.icon", "", &stype, &val)
  198.     || XrmGetResource(xrdb, "Sxpm.icon", "", &stype, &val)) {
  199.     iconFile = (char *) val.addr;
  200.     }
  201.     if (iconFile) {
  202.  
  203.     XColor color, junk;
  204.     Pixel bpix;
  205.     Window iconW;
  206.  
  207.     if (XAllocNamedColor(dpy, colormap, "black", &color, &junk))
  208.         bpix = color.pixel;
  209.     else
  210.         bpix = XBlackPixelOfScreen(screen);
  211.  
  212.     iconW = XCreateSimpleWindow(dpy, root, 0, 0,
  213.                     IWIDTH, IHEIGHT, 1, bpix, bpix);
  214.  
  215.     icon.attributes.valuemask = XpmReturnPixels;
  216.     ErrorStatus = XpmReadFileToPixmap(dpy, root, iconFile, &icon.pixmap,
  217.                       &icon.mask, &icon.attributes);
  218.     ErrorMessage(ErrorStatus, "Icon");
  219.  
  220.     XSetWindowBackgroundPixmap(dpy, iconW, icon.pixmap);
  221.  
  222.     n = 0;
  223.     XtSetArg(args[n], XtNbackground, bpix);
  224.     n++;
  225.     XtSetArg(args[n], XtNiconWindow, iconW);
  226.     n++;
  227.     XtSetValues(topw, args, n);
  228.     }
  229.     /*
  230.      * arguments parsing
  231.      */
  232.  
  233.     command = argv;
  234.     for (n = 1; n < argc; n++) {
  235.     if (strcmp(argv[n], "-plaid") == 0) {
  236.         stdinf = 0;
  237.         continue;
  238.     }
  239.     if (argv[n][0] != '-') {
  240.         stdinf = 0;
  241.         input = argv[n];
  242.         continue;
  243.     }
  244.     if ((strlen(argv[n]) == 1) && (argv[n][0] == '-'))
  245.         /* stdin */
  246.         continue;
  247.     if (strcmp(argv[n], "-o") == 0) {
  248.         if (n < argc - 1) {
  249.         if ((strlen(argv[n + 1]) == 1) && (argv[n + 1][0] == '-'))
  250.             stdoutf = 1;
  251.         else
  252.             output = argv[n + 1];
  253.         n++;
  254.         continue;
  255.         } else
  256.         Usage();
  257.     }
  258.     if (strcmp(argv[n], "-nod") == 0) {
  259.         nod = 1;
  260.         continue;
  261.     }
  262.     if (strcmp(argv[n], "-nom") == 0) {
  263.         nom = 1;
  264.         continue;
  265.     }
  266.     if (strcmp(argv[n], "-sc") == 0) {
  267.         if (n < argc - 2) {
  268.         valuemask |= XpmColorSymbols;
  269.         symbols[numsymbols].name = argv[++n];
  270.         symbols[numsymbols++].value = argv[++n];
  271.         continue;
  272.         } else
  273.         Usage();
  274.     }
  275.     if (strcmp(argv[n], "-sp") == 0) {
  276.         if (n < argc - 2) {
  277.         valuemask |= XpmColorSymbols;
  278.         symbols[numsymbols].name = argv[++n];
  279.         symbols[numsymbols].value = NULL;
  280.         symbols[numsymbols++].pixel = atol(argv[++n]);
  281.         continue;
  282.         }
  283.     }
  284.     if (strcmp(argv[n], "-cp") == 0) {
  285.         if (n < argc - 2) {
  286.         valuemask |= XpmColorSymbols;
  287.         symbols[numsymbols].name = NULL;
  288.         symbols[numsymbols].value = argv[++n];
  289.         symbols[numsymbols++].pixel = atol(argv[++n]);
  290.         continue;
  291.         }
  292.     }
  293.     if (strcmp(argv[n], "-mono") == 0) {
  294.         valuemask |= XpmColorKey;
  295.         view.attributes.color_key = XPM_MONO;
  296.         continue;
  297.     }
  298.     if (strcmp(argv[n], "-gray4") == 0 || strcmp(argv[n], "-grey4") == 0) {
  299.         valuemask |= XpmColorKey;
  300.         view.attributes.color_key = XPM_GRAY4;
  301.         continue;
  302.     }
  303.     if (strcmp(argv[n], "-gray") == 0 || strcmp(argv[n], "-grey") == 0) {
  304.         valuemask |= XpmColorKey;
  305.         view.attributes.color_key = XPM_GRAY;
  306.         continue;
  307.     }
  308.     if (strcmp(argv[n], "-color") == 0) {
  309.         valuemask |= XpmColorKey;
  310.         view.attributes.color_key = XPM_COLOR;
  311.         continue;
  312.     }
  313.     if (strncmp(argv[n], "-closecolors", 6) == 0) {
  314.         valuemask |= XpmCloseness;
  315.         view.attributes.closeness = 40000;
  316.         continue;
  317.     }
  318.     if (strcmp(argv[n], "-rgb") == 0) {
  319.         if (n < argc - 1) {
  320.         valuemask |= XpmRgbFilename;
  321.         view.attributes.rgb_fname = argv[++n];
  322.         continue;
  323.         } else
  324.         Usage();
  325.  
  326.     }
  327.     if (strcmp(argv[n], "-v") == 0) {
  328.         verbose = 1;
  329.         continue;
  330.     }
  331.     if (strcmp(argv[n], "-pcmap") == 0) {
  332.         valuemask |= XpmColormap;
  333.         continue;
  334.     }
  335.     Usage();
  336.     }
  337.  
  338.     XtRealizeWidget(topw);
  339.     if (valuemask & XpmColormap) {
  340.     colormap = XCreateColormap(dpy, win,
  341.                    DefaultVisual(dpy, DefaultScreen(dpy)),
  342.                    AllocNone);
  343.     view.attributes.colormap = colormap;
  344.     XSetWindowColormap(dpy, win, colormap);
  345.     }
  346.     view.attributes.colorsymbols = symbols;
  347.     view.attributes.numsymbols = numsymbols;
  348.     view.attributes.valuemask = valuemask;
  349.  
  350. #ifdef Debug
  351.     /* this is just to test the XpmCreateDataFromPixmap function */
  352.  
  353.     view.attributes.valuemask |= XpmReturnPixels;
  354.     view.attributes.valuemask |= XpmReturnExtensions;
  355.     ErrorStatus = XpmCreatePixmapFromData(dpy, win, plaid,
  356.                       &view.pixmap, &view.mask,
  357.                       &view.attributes);
  358.     ErrorMessage(ErrorStatus, "Plaid");
  359.  
  360.     ErrorStatus = XpmCreateDataFromPixmap(dpy, &data, view.pixmap, view.mask,
  361.                       &view.attributes);
  362.     ErrorMessage(ErrorStatus, "Data");
  363.     if (verbose && view.attributes.nextensions) {
  364.     unsigned int i, j;
  365.  
  366.     for (i = 0; i < view.attributes.nextensions; i++) {
  367.         fprintf(stderr, "Xpm extension : %s\n",
  368.             view.attributes.extensions[i].name);
  369.         for (j = 0; j < view.attributes.extensions[i].nlines; j++)
  370.         fprintf(stderr, "\t\t%s\n",
  371.             view.attributes.extensions[i].lines[j]);
  372.     }
  373.     }
  374.     XFreePixmap(dpy, view.pixmap);
  375.     if (view.mask)
  376.     XFreePixmap(dpy, view.mask);
  377.  
  378.     XFreeColors(dpy, colormap,
  379.         view.attributes.pixels, view.attributes.npixels, 0);
  380.  
  381.     XpmFreeAttributes(&view.attributes);
  382.     view.attributes.valuemask = valuemask;
  383. #endif
  384.  
  385.     if (input || stdinf) {
  386.     view.attributes.valuemask |= XpmReturnInfos;
  387.     view.attributes.valuemask |= XpmReturnPixels;
  388.     view.attributes.valuemask |= XpmReturnExtensions;
  389.  
  390. #ifdef Debug
  391.     XpmFree(data);
  392.  
  393.     /*
  394.      * this is just to test the XpmCreatePixmapFromBuffer and
  395.      * XpmCreateBufferFromPixmap functions
  396.      */
  397.     ErrorStatus = XpmReadFileToBuffer(input, &buffer);
  398.     ErrorMessage(ErrorStatus, "CreateBufferFromFile");
  399.  
  400.     ErrorStatus = XpmCreatePixmapFromBuffer(dpy, win, buffer,
  401.                         &view.pixmap, &view.mask,
  402.                         &view.attributes);
  403.     ErrorMessage(ErrorStatus, "CreatePixmapFromBuffer");
  404.     XpmFree(buffer);
  405.     ErrorStatus = XpmCreateBufferFromPixmap(dpy, &buffer,
  406.                         view.pixmap, view.mask,
  407.                         &view.attributes);
  408.     ErrorMessage(ErrorStatus, "CreateBufferFromPixmap");
  409.     ErrorStatus = XpmWriteFileFromBuffer("buffer_output", buffer);
  410.     ErrorMessage(ErrorStatus, "WriteFileFromBuffer");
  411.     XpmFree(buffer);
  412.     if (view.pixmap) {
  413.         XFreePixmap(dpy, view.pixmap);
  414.         if (view.mask)
  415.         XFreePixmap(dpy, view.mask);
  416.  
  417.         XFreeColors(dpy, colormap, view.attributes.pixels,
  418.             view.attributes.npixels, 0);
  419.  
  420.         XpmFreeAttributes(&view.attributes);
  421.     }
  422.     ErrorStatus = XpmReadFileToData(input, &data);
  423.     ErrorMessage(ErrorStatus, "ReadFileToData");
  424.     ErrorStatus = XpmCreatePixmapFromData(dpy, win, data,
  425.                           &view.pixmap, &view.mask,
  426.                           &view.attributes);
  427.     ErrorMessage(ErrorStatus, "CreatePixmapFromData");
  428.     ErrorStatus = XpmWriteFileFromData("sxpmout.xpm", data);
  429.     ErrorMessage(ErrorStatus, "WriteFileFromData");
  430.     XpmFree(data);
  431.     XpmFreeAttributes(&view.attributes);
  432. #endif
  433.  
  434.     ErrorStatus = XpmReadFileToPixmap(dpy, win, input,
  435.                       &view.pixmap, &view.mask,
  436.                       &view.attributes);
  437.     ErrorMessage(ErrorStatus, "Read");
  438.     if (verbose && view.attributes.nextensions) {
  439.         unsigned int i, j;
  440.  
  441.         for (i = 0; i < view.attributes.nextensions; i++) {
  442.         fprintf(stderr, "Xpm extension : %s\n",
  443.             view.attributes.extensions[i].name);
  444.         for (j = 0; j < view.attributes.extensions[i].nlines; j++)
  445.             fprintf(stderr, "\t\t%s\n",
  446.                 view.attributes.extensions[i].lines[j]);
  447.         }
  448.     }
  449.     } else {
  450. #ifdef Debug
  451.     ErrorStatus = XpmCreatePixmapFromData(dpy, win, data,
  452.                           &view.pixmap, &view.mask,
  453.                           &view.attributes);
  454.     XpmFree(data);
  455. #else
  456.     ErrorStatus = XpmCreatePixmapFromData(dpy, win, plaid,
  457.                           &view.pixmap, &view.mask,
  458.                           &view.attributes);
  459. #endif
  460.     ErrorMessage(ErrorStatus, "Plaid");
  461.     }
  462.     if (output || stdoutf) {
  463.     ErrorStatus = XpmWriteFileFromPixmap(dpy, output, view.pixmap,
  464.                          view.mask, &view.attributes);
  465.     ErrorMessage(ErrorStatus, "Write");
  466.     }
  467.     if (!nod) {
  468.  
  469.     /*
  470.      * manage display if requested
  471.      */
  472.  
  473.     XSizeHints size_hints;
  474.     char *xString = NULL;
  475.  
  476.     if (w_rtn && h_rtn
  477.         && ((w_rtn < view.attributes.width)
  478.         || h_rtn < view.attributes.height)) {
  479.         resize = 1;
  480.     }
  481.     if (resize) {
  482.         XtResizeWidget(topw,
  483.                view.attributes.width, view.attributes.height, 1);
  484.     }
  485.     if (incResize) {
  486.         size_hints.flags = USSize | PMinSize | PResizeInc;
  487.         size_hints.height = view.attributes.height;
  488.         size_hints.width = view.attributes.width;
  489.         size_hints.height_inc = view.attributes.height;
  490.         size_hints.width_inc = view.attributes.width;
  491.     } else
  492.         size_hints.flags = PMinSize;
  493.  
  494.     size_hints.min_height = view.attributes.height;
  495.     size_hints.min_width = view.attributes.width;
  496.     XSetWMNormalHints(dpy, win, &size_hints);
  497.  
  498.     if (input) {
  499.         xString = (char *) XtMalloc((sizeof(char) * strlen(input)) + 20);
  500.         sprintf(xString, "Sxpm: %s", input);
  501.         XStoreName(dpy, XtWindow(topw), xString);
  502.         XSetIconName(dpy, XtWindow(topw), xString);
  503.     } else if (stdinf) {
  504.         XStoreName(dpy, XtWindow(topw), "Sxpm: stdin");
  505.         XSetIconName(dpy, XtWindow(topw), "Sxpm: stdin");
  506.     } else {
  507.         XStoreName(dpy, XtWindow(topw), "Sxpm");
  508.         XSetIconName(dpy, XtWindow(topw), "Sxpm");
  509.     }
  510.  
  511.     XtAddEventHandler(topw, KeyPressMask, False,
  512.               (XtEventHandler) kinput, NULL);
  513.     XSetWindowBackgroundPixmap(dpy, win, view.pixmap);
  514.  
  515.     if (view.mask && !nom)
  516.         XShapeCombineMask(dpy, win, ShapeBounding, 0, 0,
  517.                   view.mask, ShapeSet);
  518.  
  519.     XClearWindow(dpy, win);
  520.     XMapWindow(dpy, win);
  521.     if (xString)
  522.         XtFree(xString);
  523.     XtMainLoop();
  524.     }
  525.     Punt(0);
  526. }
  527.  
  528. void
  529. Usage()
  530. {
  531.     fprintf(stderr, "\nUsage:  %s [options...]\n", command[0]);
  532.     fprintf(stderr, "%s\n", "Where options are:");
  533.     fprintf(stderr, "\n%s\n",
  534.         "[-d host:display]            Display to connect to.");
  535.     fprintf(stderr, "%s\n",
  536.         "[-g geom]                    Geometry of window.");
  537.     fprintf(stderr, "%s\n",
  538.         "[-hints]                     Set ResizeInc for window.");
  539.     fprintf(stderr, "%s\n",
  540.         "[-icon filename]             Set pixmap for iconWindow.");
  541.     fprintf(stderr, "%s\n",
  542.         "[-plaid]                     Read the included plaid pixmap.");
  543.     fprintf(stderr, "%s\n",
  544.         "[filename]                   Read from file 'filename', and from \
  545. standard");
  546.     fprintf(stderr, "%s\n",
  547.         "                             input if 'filename' is '-'.");
  548.     fprintf(stderr, "%s\n",
  549.         "[-o filename]                Write to file 'filename', and to \
  550. standard");
  551.     fprintf(stderr, "%s\n",
  552.         "                             output if 'filename' is '-'.");
  553.     fprintf(stderr, "%s\n",
  554.         "[-pcmap]                     Use a private colormap.");
  555.     fprintf(stderr, "%s\n",
  556.         "[-closecolors]               Try to use `close' colors.");
  557.     fprintf(stderr, "%s\n",
  558.         "[-nod]                       Don't display in window.");
  559.     fprintf(stderr, "%s\n",
  560.         "[-nom]                       Don't use clip mask if any.");
  561.     fprintf(stderr, "%s\n",
  562.         "[-mono]                      Use the colors specified for a monochrome visual.");
  563.     fprintf(stderr, "%s\n",
  564.         "[-grey4]                     Use the colors specified for a 4 greyscale visual.");
  565.     fprintf(stderr, "%s\n",
  566.         "[-grey]                      Use the colors specified for a greyscale visual.");
  567.     fprintf(stderr, "%s\n",
  568.         "[-color]                     Use the colors specified for a color visual.");
  569.     fprintf(stderr, "%s\n",
  570.         "[-sc symbol color]           Override color defaults.");
  571.     fprintf(stderr, "%s\n",
  572.         "[-sp symbol pixel]           Override color defaults.");
  573.     fprintf(stderr, "%s\n",
  574.         "[-cp color pixel]            Override color defaults.");
  575.     fprintf(stderr, "%s\n",
  576.         "[-rgb filename]              Search color names in the \
  577. rgb text file 'filename'.");
  578.     fprintf(stderr, "%s\n\n",
  579.         "[-v]                         Verbose - print out extensions.");
  580.     fprintf(stderr, "%s\n\n",
  581.         "if no input is specified sxpm reads from standard input.");
  582.     exit(0);
  583. }
  584.  
  585.  
  586. void
  587. ErrorMessage(ErrorStatus, tag)
  588.     int ErrorStatus;
  589.     char *tag;
  590. {
  591.     char *error = NULL;
  592.     char *warning = NULL;
  593.  
  594.     switch (ErrorStatus) {
  595.     case XpmSuccess:
  596.     return;
  597.     case XpmColorError:
  598.     warning = "Could not parse or alloc requested color";
  599.     break;
  600.     case XpmOpenFailed:
  601.     error = "Cannot open file";
  602.     break;
  603.     case XpmFileInvalid:
  604.     error = "Invalid XPM file";
  605.     break;
  606.     case XpmNoMemory:
  607.     error = "Not enough memory";
  608.     break;
  609.     case XpmColorFailed:
  610.     error = "Failed to parse or alloc some color";
  611.     break;
  612.     }
  613.  
  614.     if (warning)
  615.     printf("%s Xpm Warning: %s.\n", tag, warning);
  616.  
  617.     if (error) {
  618.     printf("%s Xpm Error: %s.\n", tag, error);
  619.     Punt(1);
  620.     }
  621. }
  622.  
  623. void
  624. Punt(i)
  625.     int i;
  626. {
  627.     if (icon.pixmap) {
  628.     XFreePixmap(dpy, icon.pixmap);
  629.     if (icon.mask)
  630.         XFreePixmap(dpy, icon.mask);
  631.  
  632.     XFreeColors(dpy, colormap,
  633.             icon.attributes.pixels, icon.attributes.npixels, 0);
  634.  
  635.     XpmFreeAttributes(&icon.attributes);
  636.     }
  637.     if (view.pixmap) {
  638.     XFreePixmap(dpy, view.pixmap);
  639.     if (view.mask)
  640.         XFreePixmap(dpy, view.mask);
  641.  
  642.     XFreeColors(dpy, colormap,
  643.             view.attributes.pixels, view.attributes.npixels, 0);
  644.  
  645.     XpmFreeAttributes(&view.attributes);
  646.     }
  647.     exit(i);
  648. }
  649.  
  650. void
  651. kinput(widget, tag, xe, b)
  652.     Widget widget;
  653.     char *tag;
  654.     XEvent *xe;
  655.     Boolean *b;
  656. {
  657.     char c = '\0';
  658.  
  659.     XLookupString(&(xe->xkey), &c, 1, NULL, NULL);
  660.     if (c == 'q' || c == 'Q')
  661.     Punt(0);
  662. }
  663.